﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Resto.CostControl.Core.Domain.Query.SupplierModule;
using Enterprise.Framework.Core.Container;
using Resto.CostControl.Core.Facade.SupplierModule.DataTransferObject;
using Resto.CostControl.Core.Domain.Model;
using Resto.CostControl.Core.Cache;
using Enterprise.Framework.Frame.Architecture;

namespace Resto.CostControl.Core.Facade.SupplierModule.FacadeObject
{
    /// <summary>
    /// 供应商FO
    /// </summary>
    public class SupplierFO : IApplicationFacadeObject
    {
        #region 私有属性

        private SupplierDQO _SupplierDQO;
        private SupplierDQO SupplierDQO
        {
            get
            {
                if (_SupplierDQO == null)
                    _SupplierDQO = ObjectCreator.Create<SupplierDQO>();

                return _SupplierDQO;
            }
        }

        #endregion

        #region 公共方法

        public virtual void AddSupplier(SupplierDTO supplierDTO)
        {
            Supplier supplier = supplierDTO.ToSupplier();
            supplier.Add();

            RestoCostDomainSession.CurrentSession.SaveChanges();

            supplierDTO.ID = supplier.ID;
        }

        public virtual void UpdateSupplier(SupplierDTO supplierDTO)
        {
            Supplier supplier = SupplierDQO.GetSupplier(supplierDTO.ID);
            supplierDTO.CopyToSupplier(supplier);

            RestoCostDomainSession.CurrentSession.SaveChanges();

            SupplierDTOCache.Remove(supplier.ID);
        }

        public virtual void DeleteSupplier(string id)
        {
            Supplier supplier = SupplierDQO.GetSupplier(id);

            supplier.Delete();

            RestoCostDomainSession.CurrentSession.SaveChanges();

            SupplierDTOCache.Remove(supplier.ID);
        }

        public virtual SupplierDTO GetSupplierDTO(string id)
        {
            Supplier supplier = SupplierDQO.GetSupplier(id);
            if (supplier.IsDelete)
                return null;

            return new SupplierDTO(supplier);
        }

        #endregion
    }
}


